<?php
namespace app\admin\controller;

use think\Db;

class Link extends Base{
      /*
       * 链接列表
       */
  public function index(){
      $list = Db::name('link')->where('is_show', 1)->order('id desc')->paginate(12);
      $this->assign('list', $list);
      return $this->fetch();
  }
    /*
     * 添加友情链接
     */
    public function addLink(){
        if(IS_POST){
            $link  = I("post.");
            $link['add_time'] = time();
            $result = Db::name('link')->insert($link);
            $data = $result ? array('status' => 1, 'msg' => '添加成功！')
                            : array('status' => 1, 'msg' => '添加失败！');

               ajaxReturn($data);
        }else{
            return $this->fetch('add_link');
        }
    }

        /*
         * 修改友情链接
         */
    public function editLink(){
        if(IS_AJAX){
            $id = I('get.id');
            $link= I('post.');
            $result =  Db::name('link')->where(array('id' => $id))->update($link);
            $data = $result ? array('status' => 1, 'msg' => '修改成功！')
                            : array('status' => 0, 'msg' => '修改失败！');

                     ajaxReturn($data);
        }else{
            $id = I('get.id');
            $info = Db::name('link')->where(array('id' => $id))->find();
            if($info){
                $this->assign('info', $info);
                return $this->fetch('edit_link');
            }else{
                $this->error('该链接不存在',U('Link/index'));
            }
        }
    }

        /*
         * 删除友情链接
         */
    public function delLink(){
        if(IS_AJAX){
                       $id = I('get.id');

            $result = Db::name('link')->where('id', 'in', $id)->delete();
                $data = $result ? array('status' => 1, 'msg' => '删除成功')
                                : array('status' => 0, 'msg' => '删除失败');

                    ajaxReturn($data);
        }
    }



}